from odoo import fields, models, api
from odoo.exceptions import UserError

POSTAL_TYPE = [('send', '发件'), ('receive', '收件'), ('both', '收发件')]


class RPCourierPostalOutlet(models.Model):
    _name = 'rp.courier.postal.outlet'
    _description = '承运商网点'
    _rec_name = 'name'

    rp_courier_id = fields.Many2one(string='承运商', comodel_name='rp.courier')

    name = fields.Char(string='名称')
    zip = fields.Char(string='邮编', required=True, index=True, copy=False)
    country_id = fields.Many2one(string='国家', comodel_name='res.country')

    # state_id = fields.Many2one(string='省', comodel_name='res.country.state')
    # city_id = fields.Many2one(string='市', comodel_name='res.city')

    type = fields.Selection(string='类型', selection=POSTAL_TYPE, default=POSTAL_TYPE[2][0])

    @classmethod
    def check_uniform(self, zip, country, name, courier):
        """
        检验唯一性
        :param zip 邮编
        :param country 国家(res.country)
        :param name 名称
        :param courier 承运商(rp.courier)
        """
        rcd = self.env['rp.courier.postal.outlet'].search([('rp_courier_id', '=', courier.id),
                                                           ('country_id', '=', country.id),
                                                           ('zip', '=', zip),
                                                           ('name', '=', name)])
        if len(rcd) > 0:
            return False
        else:
            return True

    @api.model_create_multi
    def create(self, vals_list):
        for values in vals_list:
            courier = values.get('rp_courier_id', False)
            zip = values.get('zip', False)
            # country = values.get('country_id', False)
            name = values.get('name', False)
            # state = values.get('state_id', False)
            # city = values.get('city_id', False)
            rcds = self.search([('rp_courier_id', '=', courier), ('zip', '=', zip)])
            # 如果重承运商重邮编，判断其他是否相同，相同则不创建
            if rcds:
                # if courier:
                #     rcds = rcds.filtered(lambda i: i.rp_courier_id == courier.id)
                # if country:
                #     rcds = rcds.filtered(lambda i: i.country_id == country.id)
                if name:
                    rcds = rcds.filtered(lambda i: i.name == name)
                if rcds:
                    raise UserError("数据已存在!")
        return super(RPCourierPostalOutlet, self).create(vals_list)

    # @api.constrains('name', 'country_id', 'state_id', 'city_id')
    # def _check_info(self):
    #     for s in self:
    #         if all([s.name, s.country_id, s.state_id, s.city_id]):
    #             rcds = self.env['rp.courier.postal.office'].search([('name', '=', s.name),
    #                                                                 ('country_id', '=', s.country_id.id),
    #                                                                 ('state_id', '=', s.state_id.id),
    #                                                                 ('city_id', '=', s.city_id.id)])
